package io.bdmc.modules.bss.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.bdmc.common.annotation.SysLogger;
import io.bdmc.common.utils.R;
import io.bdmc.modules.bss.entity.FibreView;
import io.bdmc.modules.bss.entity.FibreViewGroup;
import io.bdmc.modules.bss.service.IFibreViewGroupService;
import io.bdmc.modules.bss.service.IFibreViewService;
import io.bdmc.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 光缆视图组 前端控制器
 * </p>
 *
 * @author 黄露
 * @since 2021-05-21
 */

// "光缆视图组", description = "光缆视图组")
@RestController
@RequestMapping("/bss/view/group")
public class FibreViewGroupController extends AbstractController {

	@Autowired
	IFibreViewGroupService viewService;

	@Autowired
	IFibreViewService routeService;

	@GetMapping("tree")
	public R tree() {
		List<HashMap<String, Object>> map = viewService.viewTree(getDeptId().intValue());
		return R.ok().putData(map);
	}

	@GetMapping("/info")
	public R list(int viewGroupId) {
		return R.ok().putData(viewService.getById(viewGroupId));
	}

	@SysLogger("新增光缆视图组")
	@PostMapping("/add")
	public R add(@RequestBody FibreViewGroup fibreViewgroup) {
		fibreViewgroup.setCreateBy(getUser().getUsername());
		fibreViewgroup.setCreateTime(LocalDateTime.now());
		fibreViewgroup.setDeptId(getDeptId().intValue());
		Boolean b = fibreViewgroup.insert();
		return R.trueOrFalse(b);
	}

	@SysLogger("修改光缆视图组")
	@PostMapping("/update")
	public R update(@RequestBody FibreViewGroup fibreViewGroup) {
		Boolean b = fibreViewGroup.updateById();
		return R.trueOrFalse(b);
	}

	@SysLogger("删除光缆视图组")
	@PostMapping("/delete")
	public R delete(@RequestBody Integer viewGroupId) {
		if (viewGroupId > 0){
			final long count = routeService.count(new LambdaQueryWrapper<FibreView>().eq(FibreView::getViewGroupId, viewGroupId));
			if(count>0){
				return R.error("请先删除光缆视图组下的子光缆视图，再删除光缆视图组。");
			}
			viewService.removeById(viewGroupId);
		}
		return R.ok();
	}


}
